package com.mcmaster.android.menus;

import java.io.FileNotFoundException;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.mcmaster.android.Output;
import com.mcmaster.android.R;
import com.mcmaster.android.R.layout;
import com.mcmaster.android.control.GuidedTourControl;
import com.mcmaster.android.data.*;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class GuidedMenu extends MapActivity {
	
	String contents_;
	Building current_building_;
	MapController mapctrl_;
	GuidedTourControl controller_;
	
	public void onCreate(Bundle savedInstanceState) {
		Bundle extras = getIntent().getExtras();
		String tour_name_ = extras.getString("name");
		
		super.onCreate(savedInstanceState);
		setContentView(layout.guided_layout_1);
		
		MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setSatellite(true);
        
		Button guided_scan_btn = (Button)findViewById(R.id.guided_scan_btn);
		Button guided_tours_btn = (Button)findViewById(R.id.guided_tours_btn);
		
		guided_scan_btn.setOnClickListener(new ButtonHandlerScan());
		guided_tours_btn.setOnClickListener(new ButtonHandlerTourMenu());
		
		try {
			controller_ = new GuidedTourControl(this, mapView, TourDatabase.getTourFromName(tour_name_));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	private class ButtonHandlerTourMenu implements View.OnClickListener {
	    public void onClick(View v) {
	    	handleTourClick();
	    }
	}
	
	private void handleTourClick() {
		finish();
	}
	
	private class ButtonHandlerScan implements View.OnClickListener {
	    public void onClick(View v) {
				try {
					handleScanClick();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
	    }
	}
	
	private void handleScanClick() throws FileNotFoundException {	
		
		//These next two lines handle the initiation of the barcode scanner app
		IntentIntegrator integrator = new IntentIntegrator(GuidedMenu.this);
		integrator.initiateScan();
		//Upon calling the above line, focus will be taken away from our program and temporarily placed on the QR scanner
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	
	//Function - onActivityResult
	//
	// This function is called whenever an activity ends. This normally isn't useful, except when reading QR codes.
	// Essentially, the program needs to know when a code has been read. Upon reading a code (or being closed by
	// the user), the QR reading activity terminates and calls this function. The QR code itself is stored in a
	// string variable, named "contents"
	//
	@Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
      if (result != null) { //if something has actually happened
        String contents = result.getContents(); //fetch the contents
        if (contents != null) {
        	//If a QR code is scanned, the following code is executed
        	//The QR code itself is stored in a string named "contents", it's what should be passed through your functions
        	//Toast.makeText(GuidedMenu.this, contents, Toast.LENGTH_SHORT).show(); //debug line, displays what was contained in the code
        	controller_.startQRReader(contents);
        	//NOTE: Technically, you can scan things other than QR codes with the barcode scanner. If this is a problem, let Nathan know
        } else {
        	//If no QR code was detected, then the code here will be executed
        }
      }
    }
}